package com.secneo.appshare.activities;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;

public class ReplayPrivateMsgActivity extends CommonMenuActivity {

	private String mTuid;
	private String mData;
	private EditText mDataEt = null;
	private TextView mReplayBtn = null;
	private Button mRetIv = null ;
	private ProgressDialog mNetProgressBar = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.replay_private_msg);
		AppShareApplication.getInstance().addActivity(this);
		loadIntent(savedInstanceState) ;
		
		mDataEt = (EditText) findViewById(R.id.data_edit);
		mReplayBtn = (TextView) findViewById(R.id.replay_btn);
		mReplayBtn.setOnClickListener(mReplayListener);
		mRetIv = (Button)findViewById(R.id.img_ret) ;
		mRetIv.setOnClickListener(mRetListener) ;
		//mNetProgressBar = (ProgressBar) findViewById(R.id.net_progressbar);
	}

	private void loadIntent(Bundle savedInstanceState) {
		mTuid = getIntent().getExtras().getString(Constants.UID_ACTION) ;
	}

	private final OnClickListener mRetListener = new OnClickListener(){
		@Override
		public void onClick(View v){
			finish() ;
		}
	} ;
	
	private final OnClickListener mReplayListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			mData = mDataEt.getText().toString();

			if (mData == null || "".equals(mData)) {
				Toast.makeText(ReplayPrivateMsgActivity.this,
						R.string.notify_private_msg_null, Toast.LENGTH_SHORT).show();
				return;
			}
			String prompt = getResources().getString(R.string.replay_private_msg_ing) ;
			mNetProgressBar = ProgressDialog.show(ReplayPrivateMsgActivity.this, null,
					prompt ,true);
//			mNetProgressBar.setVisibility(View.INVISIBLE);
			try
			{
				mSendThread.start() ;
				
			} catch (Exception ex) {
				Log.d("MyTag", "mSendThread start() error = "+ex.getMessage());
				Message msg = new Message();
				msg.what = -1;
				mDealRetHandler.sendMessage(msg);
				ex.printStackTrace();
			}
		}
	};

	private Handler mDealRetHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
//			mNetProgressBar.setVisibility(View.GONE);
			mNetProgressBar.dismiss() ;
			switch (msg.what) {
			case 1:
				Toast.makeText(ReplayPrivateMsgActivity.this,
						R.string.replay_private_msg_succes, Toast.LENGTH_SHORT).show();
				finish() ;
				break;
			case -1:
				Toast.makeText(ReplayPrivateMsgActivity.this,
						R.string.replay_private_msg_faild, Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		}
	};

	Thread mSendThread = new Thread() { 
		public void run() {
			RemoteApiImpl api_impl = new RemoteApiImpl();
			int ret = api_impl.sendMsg(LocalStore.getLocaleStoreInstance(
					ReplayPrivateMsgActivity.this).getUid() , 
					mTuid , mData);
			Message msg = new Message();
			msg.what = ret;
			mDealRetHandler.sendMessage(msg);
		}
	};

}
